package com.medical.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.system.common.Result;
import com.medical.system.model.News;
import com.medical.system.service.SysNewsService;
import com.medical.system.vo.NewsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "公告管理")
@RequestMapping("/admin/system/news")
public class SysNewsController {

    @Autowired
    private SysNewsService sysNewsService;

    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectClientPageByVo(@PathVariable Long page,
                                       @PathVariable Long limit,
                                       NewsVo newsVo){
        IPage<News> iPage = new Page<>(page,limit);

        iPage =   this.sysNewsService.selectPage(iPage,newsVo);
        return Result.ok(iPage);
    }

    @ApiOperation("根据ID查询公告")
    @GetMapping("/findNewsById/{id}")
    public Result queryDepartmentById(@PathVariable Long id) {
        System.out.println("id = " + id);
        News news = this.sysNewsService.getById(id);
        return Result.ok(news);
    }

    @ApiOperation("修改公告")
    @PostMapping("/updateNews")
    public Result updateDepartment(@RequestBody News news) {
        boolean isSuccess = this.sysNewsService.updateById(news);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("添加新公告")
    @PostMapping("addNews")
    public Result insertDepartment(@RequestBody News news) {
        boolean saved = this.sysNewsService.save(news);
        if (saved) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("删除公告")
    @DeleteMapping("removeNewsById/{id}")
    public Result removeDepartment(@PathVariable Long id) {
        boolean removed = this.sysNewsService.removeById(id);
        if (removed) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}
